//
//  InformationViewController.m
//  TheRoadAD
//
//  Created by baixue on 16/8/17.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "InformationViewController.h"
#import "HeadImgCell.h"
#import "PhoneViewController.h"//手机号码
#import "IdentityViewController.h"//身份
#import "PersonalPhotoView.h"//个人照片
#import "VehicleListView.h"//车辆信息
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface InformationViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate>
{
    UIImagePickerController *_imagePickerController;
    UIImageView *_headImg;
}
@property (nonatomic, strong) UITableView *tableView;

@end
static BOOL isEdit;
#define TITLELABLE @[@"头像",@"昵称",@"手机号码",@"身份",@"微信号",@"性别",@"年龄",@"地区",@"学历",@"车辆信息",@"个人照片"]
#define PlorTF @[@"",@"请输入昵称",@"",@"",@"请输入微信号",@"性别",@"年龄",@"地区",@"学历",@"",@""]
@implementation InformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    [self setTableView];
    isEdit = NO;
    [self upLoadHeadImg];
    
    //导航右编辑按钮
    UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editBtn)];
    self.navigationItem.rightBarButtonItem = edit;
    
}

//点击编辑 进入编辑模式
-(void)editBtn{
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        isEdit = YES;
        [_tableView reloadData];
        self.navigationItem.rightBarButtonItem.title = @"完成";
    }else{
        isEdit = NO;
        [_tableView reloadData];
        self.navigationItem.rightBarButtonItem.title = @"编辑";
    }

}


-(void)setTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    _tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"HeadImgCell" bundle:nil] forCellReuseIdentifier:@"HeadImgCell"];
}

//cell复用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HeadImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadImgCell"];
   
    //section == 0  头像显示 否则 输入框显示
    if (indexPath.section == 0) {
        cell.headImg.hidden = NO;
    }else{
        
        cell.attributesTF.hidden = NO;
        
        cell.DidHeadImg = ^(){
            NSLog(@"bsmd");
            
        };
    }
    if(indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 9 || indexPath.section == 10){
        cell.attributesTF.hidden = YES;
        cell.accessoryType = 1;
    }
    
    if (indexPath.section == 1 || indexPath.section == 4 || indexPath.section == 7 || indexPath.section == 10) {
        cell.line.hidden = YES;
    }

    cell.attributesLable.text = TITLELABLE[indexPath.section];
    cell.attributesTF.placeholder = PlorTF[indexPath.section];
    
    //isEdit Yes 可编辑
    cell.attributesTF.enabled = isEdit;
    cell.headImg.userInteractionEnabled = isEdit;
    if (isEdit == YES) {
        cell.attributesTF.enabled = isEdit;
    }
     _headImg = cell.headImg;
    cell.DidHeadImg = ^(){
        
    };
    return cell;
}

//组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 11;
}

//行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//footer高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.5;
}
//header高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 2 || section == 5 || section == 8) {
        return 8;
        
    }else{
        return 0.5;
    }
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 ) {
        return 75 * Screen_Height/736;
    }
    return 50 * Screen_Height/736;
}

//空白收键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (isEdit == YES) {
        switch (indexPath.section) {
            case 2:
            {
                PhoneViewController *p = [[PhoneViewController alloc] init];
                [self.navigationController pushViewController:p animated:YES];
            }
                break;
            case 3:
            {
                IdentityViewController *p = [[IdentityViewController alloc] init];
                [self.navigationController pushViewController:p animated:YES];
            }
                break;
            case 9:
            {
                VehicleListView *p = [[VehicleListView alloc] init];
                [self.navigationController pushViewController:p animated:YES];
                
            }
                break;
            case 10:
            {
                PersonalPhotoView *p = [[PersonalPhotoView alloc] init];
                [self.navigationController pushViewController:p animated:YES];
                
            }
                break;
                
            default:
                break;
        }
    }
    
}

-(void)upLoadHeadImg{
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
}



@end
